/**
 * 
 */
package org.statewalker.parser.wiki.tokenizer.inline;

import org.statewalker.parser.wiki.tokenizer.base.StreamToken;
import org.statewalker.tokenizer.base.AbstractTokenizer;
import org.statewalker.tokenizer.base.ITokenFactory;
import org.statewalker.tokenizer.stream.ICharStream;
import org.statewalker.tokenizer.stream.ICharStream.IMarker;

public class FormatTokenizer extends AbstractTokenizer<StreamToken> {

    public FormatTokenizer(ITokenFactory<StreamToken> factory) {
        super(factory);
    }

    protected boolean checkChar(char ch) {
        boolean result = false;
        switch (ch) {
            case '*':
            case '_':
            case '$':
            case '^':
            case '~':
            case '+':
                // case '-':
            case '#':
                result = true;
                break;
            default:
                break;
        }
        return result;
    }

    /**
     * @see org.statewalker.tokenizer.base.Tokenizer.ITokenizer#read(org.statewalker.tokenizer.stream.ICharStream)
     */
    public StreamToken read(ICharStream stream) {
        char first = stream.getChar();
        if (!checkChar(first))
            return null;
        StreamToken result = null;
        IMarker marker = stream.markPosition();
        try {
            if (!stream.incPos())
                return result;
            char second = stream.getChar();
            if (second == first) {
                stream.incPos();
                result = newToken(stream, marker);
            }
            return result;
        } finally {
            marker.close(result == null);
        }
    }
}